
#include <iostream>
using namespace std;
int lastRemaining(unsigned int n,unsigned int m)
{
	if(n<1 || m<1)
		return -1;
	if(n==1)
		return 0;
	return (lastRemaining(n-1,m)+m)%n;
}

int main()
{
	cout<<lastRemaining(6,7)<<endl;
	return 0;
}